package com.sxz.sbs.module.client;

import android.content.Intent;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.blankj.utilcode.util.LogUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.sxz.sbs.R;
import com.sxz.sbs.applibrary.state.StateLayout;
import com.sxz.sbs.base.BaseActivity;
import com.sxz.sbs.constant.IntentConstant;
import com.sxz.sbs.entity.AuthorityEntity;
import com.sxz.sbs.http.retrofit.HttpParamMap;
import com.sxz.sbs.module.client.adapter.ClientManagementAdapter;
import com.sxz.sbs.module.client.presenter.ClientManagementPresenter;
import com.sxz.sbs.module.client.view.ClientManagementView;
import com.sxz.sbs.router.RouterUrl;
import com.sxz.sbs.widget.CommonTitle;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
/**
 * @author hp
 * @date 2018/1/9
 * 客户管理  第一级界面
 */
@Route(path = RouterUrl.CLIENT_MANGER)
public class ClientManagementActivity extends BaseActivity<ClientManagementPresenter> implements ClientManagementView,SwipeRefreshLayout.OnRefreshListener {
    @Autowired
    public int val_Level;
    @Autowired
    public int id;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.btn_add_useraccount)
    Button btnAddUseraccount;
    @BindView(R.id.stateLayout)
    StateLayout stateLayout;
    @BindView(R.id.refreshLayout)
    SwipeRefreshLayout refreshLayout;
    private List<AuthorityEntity> mlist = new ArrayList<>();
    private ClientManagementAdapter clientManagementAdapter;
    private Intent intent;

    @Override
    protected void init() {
        new CommonTitle.Builder(this).setTitle("客户管理").build().show();
        /**
         * 路由接收值注册
         */
        ARouter.getInstance().inject(this);
        getDatas();
        refreshLayout.setRefreshing(true);
        refreshLayout.setColorSchemeResources(R.color.color_main);
        refreshLayout.setOnRefreshListener(this);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        clientManagementAdapter=new ClientManagementAdapter(mlist);
        recyclerView.setAdapter(clientManagementAdapter);

        recyclerView.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void onSimpleItemClick(BaseQuickAdapter adapter, View view, int position) {
                ARouter.getInstance()
                        .build(mlist.get(position).getUrl_ClickUrl())
                        .withString("txt_Para", mlist.get(position).getTxt_Para())
                        .withString(IntentConstant.STALL_HOUSES,"")
                        .navigation();
            }
        });

        btnAddUseraccount.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(AddUserAccountActivity.class);
            }
        });
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_clientmanagement;
    }

    @Override
    protected ClientManagementPresenter createPresenter() {
        return new ClientManagementPresenter();
    }

    @Override
    public void onClientManagementSuccess(List<AuthorityEntity> data) {
        if (mIsLive && data != null) {
            refreshLayout.setRefreshing(false);
            if (data != null && data.size() > 0) {
                stateLayout.showContent();
                mlist.clear();
                mlist.addAll(data);
                for (AuthorityEntity authorityEntity: mlist) {
                    LogUtils.i(authorityEntity.getUrl_ClickUrl());
                }
                clientManagementAdapter.notifyDataSetChanged();
            }else {
                clientManagementAdapter.notifyDataSetChanged();
                //  数据为空
                stateLayout.showEmpty();
            }
        }
    }

    /**
     * 请求数据
     */
    private void getDatas(){
        HashMap<String, Object> map = HttpParamMap.getParamMap();
        map.put("fatherId", id);
        mPresenter.getClientManagementList(map);
    }

    @Override
    public void onRefresh() {
        getDatas();
    }
}
